/*******************************************************************************
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * Copyright (c) 2011 Michael Chan.
 ******************************************************************************/
package com.google.code.solrdimension.config.converters;

import java.util.ArrayList;
import java.util.List;

import com.google.code.solrdimension.config.dimensionvalue.AbstractDimensionValueConfig;
import com.google.code.solrdimension.config.dimensionvalue.ExactDimensionValueConfig;
import com.google.code.solrdimension.config.dimensionvalue.SiftDimensionValueConfig;
import com.google.code.solrdimension.parsers.beans.Dimension;
import com.google.code.solrdimension.parsers.beans.DimensionValue;
import com.google.code.solrdimension.parsers.beans.ExactDimensionValue;
import com.google.code.solrdimension.parsers.beans.SiftDimensionValue;

/**
 * This is a spring registered component, responsible for converting {@link ExactDimensionValue} 
 * to {@link ExactDimensionValueConfig}
 * 
 * @author mhc
 *
 */
public class SiftDimensionValueConverter extends AbstractDimensionConfigConverter{

    
    /**
     * @see AbstractDimensionConfigConverter
     */
    public List<? extends AbstractDimensionValueConfig> convert(List<? extends DimensionValue> dimensionValues, Dimension dimension) throws UnsupportedOperationException{
        
       List<SiftDimensionValueConfig> result = null;
        
       if (dimensionValues != null && !dimensionValues.isEmpty() && dimension != null) {
           
           if (!(dimensionValues.get(0) instanceof SiftDimensionValue)) {
               
               throw new UnsupportedOperationException("SiftDimensionValueConverter does not support the conversion of " + dimensionValues.get(0).getClass().getName());
           } else {
               
               result = new ArrayList<SiftDimensionValueConfig>();
               
               for (DimensionValue dimensionValue : dimensionValues){
                   
                   SiftDimensionValueConfig config = new SiftDimensionValueConfig();
                   
                   SiftDimensionValue siftDimVal = (SiftDimensionValue)dimensionValue;
                   
                   setCommonFields(config, siftDimVal, dimension);
                   
                   config.setMatchregex("[" + siftDimVal.getLowerbound() + "-" + siftDimVal.getUpperbound() + "].*");
                   
                   result.add(config);
                   
               }
               
           }
           
       }
       
       return result;
        
    }

}
